package a66weding.com.jiehuntong.dao.record;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import a66weding.com.jiehuntong.dao.db.TastBean;

/**
 * Created by admin on 2016/8/9.
 */
public class RecordDao  {
    private RecordOpenHelper helper;
    public RecordDao(Context context){
        helper = new RecordOpenHelper(context);
    }

    /**
     * 添加数据;
     */
    /**
     * 添加数据;
     * @param name
     * @param phone
     * @return
     */
    public long add(String name, String phone) {
        SQLiteDatabase db = helper.getWritableDatabase();
        // 其实就是一个map集合
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("keywords", phone);
        long result = db.insert("seacher", null, values);
        db.close();
        return result;
    }
    /**
     * 查找数据;
     */
    /**
     * 查找全部
     *
     * @return
     */
    public List<RecordBean> findAll() {
        SQLiteDatabase db = helper.getReadableDatabase();
        List<RecordBean> list = new ArrayList<RecordBean>();
        Cursor cursor = db.query("seacher",
                new String[] {  "name", "keywords" }, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(0);
            String keywords = cursor.getString(1);
            RecordBean bean = new RecordBean();
            bean.setKeywords(keywords);
            bean.setName(name);
            list.add(bean);
        }
        cursor.close();
        db.close();
        return list;
    }
    /**
     * 删除数据库;
     */
    public void delete(String name){
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("delete from seacher where name=?", new Object[]{name});
        db.close();
    }
}
